/**
 * Created by dengtao on 17/3/4.
 */
var WebViewJsBridge = {
    setupWebViewJavascriptBridge: function (callback) {
        var that = this;
        if (window.WebViewJavascriptBridge) {
            return callback(WebViewJavascriptBridge);
        } else {
            document.addEventListener(
                'WebViewJavascriptBridgeReady'
                , function () {
                    callback(WebViewJavascriptBridge)
                },
                false
            );
        }
        if (window.WVJBCallbacks) {
            return window.WVJBCallbacks.push(callback);
        }
        window.WVJBCallbacks = [callback];
        that.WVJBIframe = document.createElement('iframe');
        that.WVJBIframe.style.display = 'none';
        that.WVJBIframe.src = 'wvjbscheme://__BRIDGE_LOADED__';
        document.documentElement.appendChild(that.WVJBIframe);
        /*setTimeout(function () {
         document.documentElement.removeChild(WVJBIframe)
         }, 0);*/
    },

    //js获取从native传递过来的数据
    GetMsgFromNative: function (callback) {
        if (typeof callback !== 'function') {
            callback = false;
        }
        WebViewJavascriptBridge.registerHandler('GetMsgFromNative', function (data, response) {
            if (data && callback) {
                callback(data);
            }
            //response.respondWith({ 'Javascript Says':'Right back atcha!' });
        })
    },
    //获取用户登录信息
    getUserLoginInfo: function (data, callback) {
        if (typeof callback !== 'function') {
            callback = false;
        }
        WebViewJavascriptBridge.callHandler(
            'getUserLoginInfo',
            data || {},
            function (responseData) {
                if (callback && responseData) {
                    callback(responseData);
                }
            }
        )
    },
    //通知app以native方式打开新h5页面
    openNewPageH5: function (data, callback) {
        if (typeof callback !== 'function') {
            callback = false;
        }
        WebViewJavascriptBridge.callHandler(
            'openNewPageH5',
            data || {},
            function (responseData) {
                if (callback && responseData) {
                    callback(responseData);
                }
            }
        )
    },
    //通知app跳转新native页面
    openNewPageNative: function (data, callback) {
        if (typeof callback !== 'function') {
            callback = false;
        }
        WebViewJavascriptBridge.callHandler(
            'openNewPageNative',
            data || {},
            function (responseData) {
                if (callback && responseData) {
                    callback(responseData);
                }
            }
        )
    },
    goNativeCheckLogin: function (callback) {
        WebViewJsBridge.getUserLoginInfo(null, function (data) {
            callback(JSON.parse(data).memberSid);
        });
    },
    goNativeEvent: function () {
        $('[goNative]').unbind('click');
        var self = this;
        $('[goNative]').on('click', function () {
            var $this = $(this);
            if ($this[0].hasAttribute('GONATIVECHECK')) {//判断是否加入登录标识(有标识，需判断登录)
                self.goNativeCheckLogin(function (memberSid) {
                    if (!memberSid) {//未登录
                        var goNativeData = {
                            "targetFlag": "gotoLoginFlag",
                            "targetId": {}
                        };
                        WebViewJsBridge.openNewPageNative(goNativeData, function (data) {
                            console.log(data);
                        });
                        return;
                    }
                    self.goNativeFun($this);
                });
            } else {
                self.goNativeFun($this);
            }

        });
    },
    goNativeFun: function ($this) {//给元素绑定去native的事件
        var goNativeFlag = $this.attr('goNative');
        if (goNativeFlag && goNativeFlag.indexOf('http:') == 0) {//http://为跳转H5页面标记
            var goH5Title;
            if (goNativeFlag.split('?').length > 1) {
                goH5Title = tools.parseQueryString(goNativeFlag);
            }
            var goH5Data = {
                "targetUrl": goNativeFlag,
            };
            if (goH5Title) {
                goH5Data.targetTitle = goH5Title.title;
            }
            WebViewJsBridge.openNewPageH5(goH5Data, function (data) {
                console.log(data);
            });
        } else if (goNativeFlag && goNativeFlag.indexOf('native:') == 0) {//native:为跳转原生页面标记
            var goNativeParams = tools.parseQueryString(goNativeFlag);
            var goNativeData = {
                "targetFlag": goNativeFlag.split(':')[1].split('?')[0],
                "targetId": goNativeParams
            };

            WebViewJsBridge.openNewPageNative(goNativeData, function (data) {
                console.log(data);
            });
        }
    },
    init: function () {
        this.setupWebViewJavascriptBridge(function (bridge) {
            bridge.init(function (message, responseCallback) {
                console.log('JS got a message', message);
                var data = {
                    'Javascript Responds': 'Wee!'
                };
                console.log('JS responding with', data);
                responseCallback(data);
            });

        });
        this.goNativeEvent();
    }
}

WebViewJsBridge.init();
